import React, { Component } from 'react'
import './index.css'

export default class Child extends Component {
  // 子传父：id、price
  /* handleClick = () => {
    this.props.changePrice(this.props.id, price)
  } */
  // !#2 子组件调用传递过来的方法的同时并传参
  handleClick = (id) => {
    // 1 ~ 5
    // Math.random() => [0, 1)
    // Math.random() * 5 => [0, 5)
    // Math.random() * 5 + 1 => [1, 6)
    const price = Math.floor(Math.random() * 5 + 1)
    this.props.changePrice(id, price)
  }
  render() {
    const { name, price, info, id } = this.props

    return (
      <div className='child'>
        <h3 className='title'>标题：{name}</h3>
        <p className='price'>价格：{price.toFixed(2)}</p>
        <p className='product'>{info}</p>
        <button onClick={() => this.handleClick(id)}>砍一刀</button>
      </div>
    )
  }
}
